#include <QtGui/QApplication>
#include "mainwindow.h"
#include "logicmanager.h"
#include "networkmanager.h"

void setupUiAndLogicConnection(MainWindow *, LogicManager *);
void setupNetworkAndLogicConnection(NetworkManager *, LogicManager *);

int main(int argc, char *argv[])
{
    QApplication app(argc, argv);

    NetworkManager *networkManager = new NetworkManager();
    LogicManager *logicManager = new LogicManager();
    MainWindow *uiWindow = new MainWindow();

    // Setup Connection
    setupUiAndLogicConnection(uiWindow, logicManager);
    setupNetworkAndLogicConnection(networkManager, logicManager);

    uiWindow->show();
    uiWindow->login();
    int appReuslt = app.exec();

    // Ui Disappear and wait logout response
    // TODO:

    // Logout finished
    delete uiWindow;
    delete logicManager;
    delete networkManager;

    return appReuslt;
}

void setupNetworkAndLogicConnection(NetworkManager * networkManager, LogicManager * logicManager)
{
    QObject::connect(networkManager, SIGNAL(recieveServerData(QByteArray)),
                     logicManager, SLOT(onReceivedServerData(const QByteArray &)));
    QObject::connect(networkManager, SIGNAL(serverNotify(int)),
                     logicManager, SLOT(onServerNotify(int)));

    QObject::connect(logicManager, SIGNAL(sendServerData(const QByteArray &)),
                     networkManager, SLOT(onSendServerData(QByteArray)));


    QObject::connect(networkManager, SIGNAL(recieveData(QString, QByteArray)),
                     logicManager, SLOT(onReceivedData(const QString &, const QByteArray &)));
    QObject::connect(networkManager, SIGNAL(notify(QString, int)),
                     logicManager, SLOT(onNotify(const QString &, int)));

    QObject::connect(logicManager, SIGNAL(sendData(const QString &, const QByteArray &)),
                     networkManager, SLOT(onSendData(QString, QByteArray)));


}

void setupUiAndLogicConnection(MainWindow * uiWindow, LogicManager * logicManager)
{
    // Ui to Logic
    QObject::connect(uiWindow, SIGNAL(closeWindow()),
                     logicManager, SLOT(onSendLogout()));
    QObject::connect(uiWindow, SIGNAL(sendMessage(const QString&, const QString&)),
                     logicManager, SLOT(onSendMessage(const QString &, const QString &)));
    QObject::connect(uiWindow, SIGNAL(stopChating(const QString&)),
                     logicManager, SLOT(onCloseConnection(const QString&)));
    QObject::connect(uiWindow, SIGNAL(login(const QString&)),
                     logicManager, SLOT(onSendLogin(const QString &)));

    // Logic to Ui
    QObject::connect(logicManager, SIGNAL(networkStatusChanged(eNetworkStatus)),
                     uiWindow, SLOT(onNetworkStatusChanged(eNetworkStatus)));
    QObject::connect(logicManager, SIGNAL(loginStatusReceived(eLoginStatus)),
                     uiWindow, SLOT(onLoginResultReceived(eLoginStatus)));
    QObject::connect(logicManager, SIGNAL(userListChanged(const QList<userInfo> &)),
                     uiWindow, SLOT(onContactListUpdated(const QList<userInfo>&)));
    QObject::connect(logicManager, SIGNAL(messageUpdate(const QString &, const QList<recordInfo> &)),
                     uiWindow, SLOT(onMessageUpdated(const QString&, const QList<recordInfo>&)));
}
